<?php

namespace app\xs_article\controller\api;

use app\xs_article\facade\service\ArticleServiceFacade;
use xsframe\base\ApiBaseController;

class Article extends ApiBaseController
{
    public function getList(): \think\response\Json
    {
        $categoryId = intval($this->params['cateid']);

        $articleList = ArticleServiceFacade::getList(['cateid' => $categoryId, 'uniacid' => $this->uniacid, 'enabled' => 1, 'deleted' => 0], "id,title,description,thumb,article_link");
        $articleList = set_medias($articleList, ['thumb']);

        $result = [
            'articleList' => $articleList
        ];
        return $this->success($result);
    }

    public function getInfo(): \think\response\Json
    {
        $articleId = intval($this->params['id']);

        $field = "id,title,showtime,is_original,article_mp,author,content,viewcount,viewcount_v,audio_url";
        $articleInfo = ArticleServiceFacade::getInfo(['id' => $articleId, 'uniacid' => $this->uniacid, 'enabled' => 1, 'deleted' => 0], $field);

        $articleInfo['showtime'] = date('Y-m-d H:i:s', $articleInfo['showtime']);

        $newViewCount = $articleInfo['viewcount'] + $articleInfo['viewcount_v'] + 1;
        ArticleServiceFacade::updateInfo(['viewcount' => $articleInfo['viewcount'] + 1], ['id' => $articleId]);
        $articleInfo['viewcount'] = $newViewCount;

        $result = [
            'articleInfo' => $articleInfo
        ];
        return $this->success($result);
    }
}
